<?php
/*
** etc: 用户模块
** author: deepe.xiao
** version: v0.1
*/

namespace app\controllers\Project_1_0;
use Core\Controller;
use \app\models\Project_1_0\User as MUser;




class User extends Controller{



    //进入登录页面
    //index.php?action=user&func=login
    function login(){

        $lib = loadLibrary('Mytp');
        // 赋值
        $lib->assign("baseurl", config_item('base_url'));
        // 渲染页面
        $lib->tp("user/login");
    }
    //登录的功能实现
    //index.php?action=user&func=signin
    function signin(){

        if (isset($_POST['Name'])&&!empty($_POST['Name'])){
            if(isset($_POST['password'])&&!empty($_POST['password'])) {

                $name = $_POST['Name'];
                $password = $_POST['password'];
                $muser = new MUser();
                $resuslt = $muser->searchUser($name,$password);

                if($resuslt){
                    //当用户30分钟,再次访问登录页是,可以直接进入首页,不用再次登录
                    //了解$_SESSION

                    $_SESSION['user'] = $resuslt;//会存放30分钟
                    // echo 1;

                   $this->success("登录成功","index.php?action=cases&func=index");
                }else{
                   $this->error("登录失败,请检查账号或密码!");
                    // echo -1;
                    // echo "登陆失败,请检查账号密码";
                }

            }else{
               $this->error("请输入密码");
                // echo -1;
                // echo "请输入密码";
            }
        }else{
           $this->error("请输入用户名");

            // echo -1;
            // echo "请输入用户名";
        }
    }

    //注册页面与注册功能
    function reg(){
        if ($_SERVER["REQUEST_METHOD"] == "GET"){
            $mytp = loadLibrary("Mytp");
            $mytp->tp("user/reg");
        }else {
            if (empty($_POST['Name']) ) {
                $this->error("请输入手机号码!");
                exit();
            }
            if ( empty($_POST['password']) ) {
                $this->error("请输入正确的密码!");
                exit();
            }

            $tel = $_POST['Name'];

            $muser = new MUser();
            if ($muser->searchUserByName($tel)){
                $this->error("该用户已存在,请重新注册!");
                exit();
            }

            $password = $_POST['password'];

            $user = $muser->register($tel,$password);
            if ($user){
                $this->success("注册成功","index.php?action=user&func=login");
            }else{
                $this->error("注册失败");

            }
        }
    }


    //注册页面与注册功能
//https://framework.deepe.ren/index.php?action=user&func=add

    function add(){
        if ($_SERVER["REQUEST_METHOD"] == "GET"){
            $mytp = loadLibrary("Mytp");
            $mytp->tp("user/reg");
        }else {
            if (empty($_POST['Name']) ) {
                $this->error("请输入手机号码!");
                exit();
            }
            if ( empty($_POST['password']) ) {
                $this->error("请输入正确的密码!");
                exit();
            }
            if ( empty($_POST['sex']) ) {
                $this->error("请选择性别!");
                exit();
            }
            if ( empty($_POST['address']) ) {
                $this->error("请选择城市!");
                exit();
            }

            if ( empty($_POST['likes']) ) {
                $this->error("请选择擅长位置!");
                exit();
            }

            $tel = $_POST['Name'];

            $muser = new MUser();
            if ($muser->searchUserByName($tel)){
                $this->error("该用户已存在,请重新注册!");
                exit();
            }

            $_POST['likes'] = implode(",",$_POST['likes']);

            $data = $_POST;

            $data['c_ip'] = $_SERVER['REMOTE_HOST'].$_SERVER['SERVER_ADDR'];
           $data['ua'] = $_SERVER['HTTP_USER_AGENT'];

            $user = $muser->register2($data);
            if ($user){
                $this->success("添加成功","index.php?action=user&func=login");
            }else{
                $this->error("添加失败");

            }
        }
    }
    function index(){
        echo "这是登陆成功后的页面";

        die();

    }


//    function
    //1找回密码页面
    function forgetpass(){
        //请求方法是GET进入1页面,POST进入2功能
        if ($_SERVER["REQUEST_METHOD"] == "GET"){
            $mytp = loadLibrary("Mytp");
            $mytp->tp("user/forgetpass");
        }else{
            if (empty($_POST['tel']) ) {
                $this->error("请输入手机号码!");
                exit();
            }
            if ( strlen($_POST['tel']) < 11) {
                $this->error("请输入手机号码!");
                exit();
            }
            $tel = $_POST['tel'];

            //确认是否存在该用户
            $muser = new MUser();
            $user = $muser->searchUserByName($tel);

            if (!$user){
                $this->error("对不起,不存在该用户!");
                exit();
            }
            $vcode = rand(1000, 9999);
            //将手机号和验证码都入会话,方便后面页面显示
            $_SESSION['tel'] = $tel;
            $_SESSION['vcode'] = $vcode;

            //操作腾讯云发送短信验证机制
            $mysms = loadLibrary("Mysms");
            $res = $mysms->sendsms($tel,$vcode,216979);

            if ($res['result'] == 0) {
                $this->success("发送短信成功", "index.php?action=user&func=verifycode");
            } else {
                $this->error("发送失败".json_encode($res),null,10);
            }

        }
    }

    //2进入验证页面
    function verifycode(){
        if ($_SERVER["REQUEST_METHOD"] == "GET"){
            $mytp = loadLibrary("Mytp");
            $mytp->tp("user/verifycode");
        }else{

            if (empty($_POST['vcode'])) {
                $this->error("请输入验证码!");
                exit();
            }
            if (empty($_SESSION['vcode'])) {
                $this->error("验证码已超时,请重新发送!");
                exit();
            }
           //用户输入POST的验证码与会话(session)存储的验证码比较
            if ($_POST['vcode'] ==  $_SESSION['vcode']) {
                $this->success("验证成功,请在30分钟内设置新密码","index.php?action=user&func=resetpass");
            }else{
                $this->error("验证失败");
            }


        }
    }

    //3进入重置新密码页面
    function resetpass(){
        if ($_SERVER["REQUEST_METHOD"] == "GET"){
            $mytp = loadLibrary("Mytp");
            $mytp->tp("user/resetpass");
        }else{

            if (empty($_SESSION['tel'])) {
                $this->error("重置密码超时","index.php?action=user&func=verifycode");
                exit();
            }
            if (empty($_POST['newpassword'])) {
                $this->error("请填写新密码!");
                exit();
            }
            if (empty($_POST['newpassword2'])) {
                $this->error("请填写确认新密码!");
                exit();
            }
            if ($_POST['newpassword'] != $_POST['newpassword2']  ) {
                $this->error("请确认两次密码是否一致!");
                exit();
            }
            //用户输入POST的验证码与会话(session)存储的验证码比较
            $tel = $_SESSION['tel'];
            $pass = $_POST['newpassword'];
            $muser =  new MUser();
            $res = $muser->updatePassByName($tel,$pass);
            if ($res) {
                $this->success("重置成功,请重新登陆","index.php?action=user&func=login");
            }else{
                $this->error("重置失败");
            }


        }
    }


    function all(){

        $muser =  new MUser();
        $json = $muser->userAll();

        echo "<table border='1'>";

        foreach ($json as $row){
            echo "<tr>";

            echo "<td>{$row['user_id']}</td>";
            echo "<td>{$row['Name']}</td>";
            echo "<td>{$row['password']}</td>";
            echo "<td>{$row['sex']}</td>";
            echo "<td>{$row['likes']}</td>";
            echo "<td>{$row['address']}</td>";
            echo "<td>{$row['c_ip']}</td>";
            echo "<td>{$row['ua']}</td>";

            echo "</tr>";
        }




echo "</table>";
    }


    function a1(){

        $start_time = microtime(true);
        echo "开始时间:".$start_time."<br>";
        for ($i=0;$i<100*10000;$i++){
//            $_SESSION['a'.$i] = $i;
        }
        $end_time = microtime(true);
        echo "结束时间:".$end_time."<br>";
        echo "共耗时:".($end_time - $start_time) ."毫秒";

    }

    //index.php?action=user&func=a2
    function a2(){

        $arr = ["lala","哈哈","lelle","呵呵"];
        loadSDK("RedisClient");
        $redis = new \RedisClient();
//            $redis->set('a',$arr);


        $redis->flushAll();

    }

    //查询排行榜
    //index.php?action=user&func=rank
//    html : {"":""}
    function rank(){

        $muser = new MUser();
        $res = $muser->selectTop();
        if ($res){
            $code = 1;
            $message = "查询成功";
        }else{
            $code = -1;
            $message = "查询失败";
        }

        $data = array("code"=>$code,"message"=>$message,"data"=>$res);

        //一定返回对象:
        //1、很多只会解析对象(只讲过怎么解析对象),不会解析数组
        //2、为了方便客户端判断查询是否成功,所以加入code
        //3、像登录功能:登录成功、账号或密码错误、账号不能为空、密码不能为空

        echo json_encode($data);


    }

    //2、更新用户的最高分数
    //支持 GET/POST
    //index.php?action=user&func=updateScore&user_id=1&score=10
    function updateScore(){

        $muser = new MUser();
        $user_id = $_REQUEST['user_id'];
        $score = $_REQUEST['score'];

        //判断是否超过了原来的分数
        $oldscore = $muser->selectScoreByUserId($user_id);

        if ($score > $oldscore['score']){

            //超过更改
            $res = $muser->updateScore($user_id,$score);
            if ($res){
                $code = 1;
                $message = "更新分数成功";
            }else{
                $code = -1;
                $message = "更新分数失败";
            }
        }else{
            //未超过不更改
            $code = -1;
            $message = "未超过原来的分数";
            $res = [];
        }
        $data = array("code"=>$code,"message"=>$message,"data"=>$res);
        echo json_encode($data);


    }

    //3、金币|积分的增加或减少
    //update t_user set score=score+8 where user_id = 1;
    //update t_user set score=score-8 where user_id = 1;



    //4、存储用户的最高分数
    //支持 GET/POST
    //index.php?action=user&func=saveScore&name=大胖子&score=10
    function saveScore(){

        $muser = new MUser();
        $name = $_REQUEST['name'];
        $score = $_REQUEST['score'];


        $user = $muser->searchUserByName($name);
        //判断是否超过了原来的分数

        $code = 1;
        $message = "";
        if ($user){
            $user_id = $user['user_id'];
            $oldscore = $muser->selectScoreByUserId($user_id);
            if ($score > $oldscore['score']){
                //超过更改
                $res = $muser->updateScore($user_id,$score);
                if ($res){
                    $code = 1;
                    $message = "更新分数成功";
                }else{
                    $code = -1;
                    $message = "更新分数失败";
                }
            }else{
                //未超过不更改
                $code = -1;
                $message = "未超过原来的分数";
                $res = [];
            }
        }else{

                $ires = $muser -> registerByScore($name,$score);
                $code = 1;
                $message = "存储完成(首玩)";
                $res = $ires;
        }



        $data = array("code"=>$code,"message"=>$message,"data"=>$res);
        echo json_encode($data);


    }


}
		